package com.jinke.api.common.annotation;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Optional;

/**
 * @projectName:
 * @title: AtLeastOneNotNullValidator
 * @description: 自定义校验注解 校验两个参数不能同时为空 具体规则
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/7/19 11:10
 */
@Slf4j
public class AtLeastOneNotNullValidator implements ConstraintValidator<AtLeastOneNotNull, Object[]> {

    @Override
    public void initialize(AtLeastOneNotNull constraintAnnotation) {}

    @Override
    public boolean isValid(Object[] objects, ConstraintValidatorContext context) {
        log.info("this is data:{}", objects);
        if (!Optional.ofNullable(objects).isPresent() || objects.length!= 2) {
            return false;
        }
        return objects[0]!= null || objects[1]!= null;
    }
}
